/**
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 *
 *AppUserUseInfoController
 * 用户使用信息
 *
 * 修改纪录
 * 2022-12-23 版本：1.0 wuyuyang 创建。
 * @version 版本：1.0
 * @author 作者：wuyuyang
 * 创建日期：2022-12-23
 */

package com.topscomm.appv2.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.web.bind.annotation.*;

import com.topscomm.appv2.controller.auto.AppUserUseInfoControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wuyuyang
 * @date: 2022-12-23
 * @description:用户使用信息Controller
 * @modify:
 */
@RestController
@RequestMapping("appv2/appUserUseInfo")
public class AppUserUseInfoController extends AppUserUseInfoControllerAuto implements IAppUserUseInfoController {
	/**
	 * @author: wuyuyang
	 * @date: 2022-12-23
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
//		getUserPermissions(paraMap);
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (usercode like '%{0}%' or username like '%{0}%') ", value);
		}
		if (paraMap.containsKey("qry_dept")) {
			String value = paraMap.get("qry_dept").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptname like '%{0}%') ", value);
		}
		if (paraMap.containsKey("qry_company")) {
			String value = paraMap.get("qry_company").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (companyname like '%{0}%') ", value);
		}
		if (paraMap.containsKey("qry_versionname")) {
			String value = paraMap.get("qry_versionname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (versionname like '%{0}%') ", value);
		}
		if (paraMap.containsKey("qry_versioncode")) {
			String value = paraMap.get("qry_versioncode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and versioncode like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_devicetype")) {
			String value = paraMap.get("qry_devicetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and devicetype like '%{0}%' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: wuyuyang
	 * @date: 2022-12-23
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			int totalCount = this.service.getCount(sqlWhere);
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord());
			} else
				tempList = this.service.queryMapByWhere(sqlWhere);
			// this.service.setDispFields(tempList);
			for(Map<String, Object> temp : tempList){
				List<Map<String, Object>> list = (List<Map<String, Object>>) JSONArray.parse(ConvertUtil.convertToString(temp.get("corearea")));
				if(list!=null && list.size()>0){
					String corearea = list.stream().map(s ->ConvertUtil.convertToString(s.get("name"))).collect(Collectors.joining(","));
					temp.put("corearea",corearea);
				}
				list = (List<Map<String, Object>>) JSONArray.parse(ConvertUtil.convertToString(temp.get("recentlyusedarea")));
				if(list!=null && list.size()>0){
					String recentlyusedarea = list.stream().map(s ->ConvertUtil.convertToString(s.get("name"))).collect(Collectors.joining(","));
					temp.put("recentlyusedarea",recentlyusedarea);
				}
				list = (List<Map<String, Object>>) JSONArray.parse(ConvertUtil.convertToString(temp.get("firstleveldirectory")));
				if(list!=null && list.size()>0){
					String firstleveldirectory = list.stream().map(s ->ConvertUtil.convertToString(s.get("name"))).collect(Collectors.joining(","));
					temp.put("firstleveldirectory",firstleveldirectory);
				}
				list = (List<Map<String, Object>>) JSONArray.parse(ConvertUtil.convertToString(temp.get("extendfield1")));
				if(list!=null && list.size()>0){
					String extendfield1 = list.stream().map(s ->ConvertUtil.convertToString(s.get("name"))).collect(Collectors.joining(","));
					temp.put("extendfield1",extendfield1);
				}
			}
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @param listModule
	 * @return
	 * @Title:
	 * @author: hudeyong
	 * @return_type: List<Map < String, Object>>
	 * @Description: 用户权限接口
	 */
	@GetMapping("/permissions")
	public ResponseResult<Object> getUserPermissions(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resultMap = this.service.getUserPermissions(paraMap);
			return ResponseResult.ok(resultMap);
		} catch (Exception var8) {
			return this.handleControllerException(var8);
		}
	}

	/***
	 * @description: 更新用户金刚区、最近使用、一级目录排序、文档中心
	 * @param paraMap
	 * @return
	 * @author: hudeyong
	 * @date:
	 * @modify:
	 */
	@PutMapping(value = "/updateUserAppInfo")
	public ResponseResult<Object> updateUserAppInfo(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.updateUserAppInfo(paraMap);
			return ResponseResult.ok("ok");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @param listModule
	 * @return
	 * @Title:
	 * @author: hudeyong
	 * @return_type: List<Map < String, Object>>
	 * @Description: 查询用户模块
	 */
	@PostMapping(value = "/queryUserModule")
	public ResponseResult<Object> queryUserModule(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resultMap = this.service.queryUserModule(paraMap);
			return ResponseResult.ok(resultMap);
		} catch (Exception var8) {
			return this.handleControllerException(var8);
		}
	}
}

